package cn.appsys.controller.back;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import cn.appsys.constant.Constant;
import cn.appsys.pojo.BackendUser;
import cn.appsys.service.back.user.BackAppUserService;

@Controller
@RequestMapping("/backlogin")
public class BackLoginController {

	@Resource
	private BackAppUserService backAppUserService;

	/**
	 * 跳转到后台管理登录页面
	 * 
	 * @return
	 */
	@RequestMapping("/NoBackend")
	public String NoBackend() {
		return "backendlogin";
	}

	/**
	 * 后台管理登录操作
	 */
	@RequestMapping("/dologin")
	public String BackendLogin(
			@RequestParam(value = "userCode") String userCode,
			@RequestParam(value = "userPassword") String userPassword,
			Model model, HttpSession session) {
		BackendUser backenduser = backAppUserService.queryBackendPwd(userCode);
		if (backenduser == null) {
			model.addAttribute("error", "用户名不存在,请确认后重新输入!");
			return "backendlogin";// 登录失败跳转到登录页面
		} else if (!backenduser.getUserPassword().equals(userPassword)) {
			model.addAttribute("error", "您输入的密码不正确,请确认后在输入");
			return "backendlogin";// 登录失败跳转到登录页面
		} else {
			// 登录成功
			session.setAttribute(Constant.USER_SESSION, backenduser);
			return "backend/main";// 登录成功跳转到
		}
	}

	// 注销
	@RequestMapping("/removeLogin")
	public String logout(HttpSession session) {
		session.removeAttribute(Constant.USER_SESSION);
		return "redirect:/backlogin/NoBackend";
	}
}
